<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\AuctionOrder;
use AuctionCore\System\AuctionException;
/**
* 通用性的拍卖设置
* @author       Eric Lee
*/
class   AuctionSetting {
        private $order_id = null;

        public function __construct(){
        }
        /**
        * 新增设置
        * @param    float $data   设置项
        * @return   boolean
        */
        public function add($data) {

               if(empty($data['name'])){
                  AuctionException::make_throw('100059');
               }
               $c = M('setting')->where("name='".$data['name']."'")->find();
               if($c){
                  AuctionException::make_throw('100062');
               }
               if(empty($data['value'])){
                  AuctionException::make_throw('100061');
               }
               $data['createtime'] = date('Y-m-d H:i:s');
               $data['updatetime'] = date('Y-m-d H:i:s');
               return M('setting')->add($data);
        }

        /**
        * 设置过户费/佣金/其它一些拍卖系统公共设置 (如过户费设置名为 TRANSFER_FEE 佣金费设置名为 COMMISION)
        * @param    float $cond   条件
        * @param    float $data   设置项
        * @return   boolean
        */
        public function update($cond,$data) {
               $data['updatetime'] = date('Y-m-d H:i:s');
               return M('setting')->where($cond)->save($data);
        }
        /**
        * 修改配置
        * @param    float $data   设置项
        * @return   boolean
        */
        public function edit($data) {
             $id = $data['id'];

             if(empty($id)){
                AuctionException::make_throw('100063');
             }
             if(empty($data['name'])){
                AuctionException::make_throw('100059');
             }
            //查有效
             $c = M('setting')->find($id);
             if(!$c){
                AuctionException::make_throw('100060');
             }
             //查重
             if($c['name']==$data['name'] && $id != $c['id']){
                AuctionException::make_throw('100062');
             }
             if(empty($data['value'])){
                AuctionException::make_throw('100061');
             }
             $data['updatetime'] = date('Y-m-d H:i:s');
             return M('setting')->where("id='".$data['id']."'")->save($data);
        }

        /*
         * 配置详细
         * @param $id 配置ID
         * @return boolean
         * */
        public function detail($id){
            $c = M('setting')->find($id);
            if(!$c){
               AuctionException::make_throw('100060');
            }
            return $c;
        }
        /*
        * 根据配置名，读取配置信息
        * @param $config_name 配置name
        * @return array 
        */
        public function detail_by_name($config_name){
            $c = M('setting')->where("name='{$config_name}'")->find();
            if(!$c){
               AuctionException::make_throw('100060');
            }
            return $c;
        }

        /*
         * 删除配置
         * @param $id 配置ID
         * @return boolean
         * */
        public function del($id){
            $c = M('setting')->find($id);
            if(!$c){
               AuctionException::make_throw('100060');
            }
            return M('setting')->delete($id);
        }

}

?>